package com.iemes.controller.metals;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.AuditLoggingFormMap;
import com.iemes.entity.DeliverynoteFormMap;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.MetalsBomFormMap;
import com.iemes.entity.PartsReturnFormMap;
import com.iemes.entity.PurchaseRequestFormMap;
import com.iemes.entity.PurchaseSesaleStockFromMap;
import com.iemes.entity.SuppFilleFromMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.DepartmentMapper;
import com.iemes.mapper.SuppfilleMapper;
import com.iemes.mapper.UserMapper;
import com.iemes.mapper.metals.AuditLoggingMapper;
import com.iemes.mapper.metals.DeliverynoteMapper;
import com.iemes.mapper.metals.MetalsBomMapper;
import com.iemes.mapper.metals.PartsReturnMapper;
import com.iemes.mapper.metals.PurchaseRequestMapper;
import com.iemes.mapper.metals.PurchaseSaleStockMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.test.JSONAssert;

@RequestMapping("parts")
@Controller
public class PartsController extends BaseController {

	@Inject
	private PartsReturnMapper partsReturnMapper;

	@Inject
	private DepartmentMapper departmentMapper;

	@Inject
	private MetalsBomMapper metalsBomMapper;

	@Inject
	private PurchaseRequestMapper purchaseRequestMapper;

	@Inject
	private DeliverynoteMapper deliverynoteMapper;

	@Inject
	private PurchaseSaleStockMapper purchaseSaleStockMapper;

	@Inject
	private AuditLoggingMapper auditLoggingMapper;

	@Inject
	private UserMapper userMapper;

	@Inject
	private SuppfilleMapper suppfilleMapper;

	public static String judgeDate(Date date) {
		Calendar date2 = Calendar.getInstance();
		date2.setTime(date);
		if (date2.get(Calendar.DATE) == date2.getActualMinimum(Calendar.DAY_OF_MONTH)) {
			return "MinDate";
		} else if (date2.get(Calendar.DATE) == date2.getActualMaximum(Calendar.DAY_OF_MONTH)) {
			return "MaxDate";
		}
		return "false";
	}

	@RequestMapping("parts-return")
	public String partsReturn(Model model) {
		PurchaseRequestFormMap purchaseRequestFormMap = new PurchaseRequestFormMap();
		purchaseRequestFormMap.put("where", "where state=2 group by buyId");
		List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper
				.findByWhere(purchaseRequestFormMap);
		model.addAttribute("purchaseRequest", purchaseRequestFormMaps);
		return Common.BACKGROUND_PATH + "/metals/parts/parts_return_add";
	}

	@RequestMapping("parts-return-list")
	public String partslist(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/metals/parts/parts_return_list";
	}

	@RequestMapping("parts-deliverynote")
	public String partsDeliverynote(Model model) {
		PurchaseRequestFormMap purchaseRequestFormMap = new PurchaseRequestFormMap();
		purchaseRequestFormMap.put("where", "where state=2 GROUP BY buyId");
		List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper
				.findByWhere(purchaseRequestFormMap);
		model.addAttribute("purchaseRequest", purchaseRequestFormMaps);
		return Common.BACKGROUND_PATH + "/metals/parts/parts_deliverynote_add";
	}

	@RequestMapping("SesaleStock")
	public String SesaleStock(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/metals/parts/parts_sell_list";
	}
	
	
	@RequestMapping("parts_sell_edit")
	public String editUI(Model model) throws Exception 
	{
		String[] ds=null;
		String[] id = getParaValues("id");
		ds = id[0].split(",");
			model.addAttribute("lastNumber", ds[13]);
			model.addAttribute("lastPrice", ds[14]);
			model.addAttribute("lastMoney", ds[15]);
			model.addAttribute("materialCode", ds[1]);	
		return Common.BACKGROUND_PATH + "/metals/parts/parts_sell_edit";
	}

	@ResponseBody
	@RequestMapping(value = "parts-deliverynote_choose")
	public Object emetalsbom_choose(Model model, String buyId) throws Exception {
		PurchaseRequestFormMap purchaseRequestFormMap = getFormMap(PurchaseRequestFormMap.class);
		purchaseRequestFormMap.put("where", "where buyId=" + buyId);
		List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper
				.findByWhere(purchaseRequestFormMap);
		for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {
			MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id", pur.get("metalsBomId").toString(),
					MetalsBomFormMap.class);
			pur.put("metalsBom", metalsBomFormMap);
		}
		return purchaseRequestFormMaps;

	}

	@ResponseBody
	@RequestMapping("parts_add")
	@SystemLog(module = "申请进仓", methods = "新增进仓") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String parts_add() throws Exception {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		String[] buyId = request.getParameterValues("buyId");
		List<PartsReturnFormMap> partsReturnFormMap = partsReturnMapper.findByAttribute("buyId", buyId[0],
				PartsReturnFormMap.class);
		if (partsReturnFormMap.size() != 0) {
			return "采购单已申请退货";
		}
		String[] id = request.getParameterValues("id");
		String[] materialCode = request.getParameterValues("materialCode");
		String[] number = request.getParameterValues("number");
		String[] total = request.getParameterValues("total");
		String[] comment = request.getParameterValues("comment");

		PurchaseRequestFormMap purchaseRequestFormMap = new PurchaseRequestFormMap();
		purchaseRequestFormMap.put("where", "where buyId=" + buyId[0]);
		List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper
				.findByWhere(purchaseRequestFormMap);
		for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {

			MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id", pur.get("metalsBomId").toString(),
					MetalsBomFormMap.class);
			pur.put("metalsBom", metalsBomFormMap);
		}

		if (null != id) {
			DeliverynoteFormMap deliverynoteFormMap = new DeliverynoteFormMap();
			int number2 = 0;
			int total2 = 0;
			for (int i = 0; i < id.length; i++) {
				number2 = number2 + new Integer(number[i]);
				total2 = total2 + new Integer(total[i]);
			}
			deliverynoteFormMap.set("godownEntryDate", DateUtils.getStringDateTime());
			deliverynoteFormMap.set("purchaseRequestId", buyId[0]);
			deliverynoteFormMap.set("number", number2);
			deliverynoteFormMap.set("total", total2);
			deliverynoteFormMap.set("comment", comment[0]);
			deliverynoteFormMap.set("state", "0");
			deliverynoteMapper.addEntity(deliverynoteFormMap);

			return "success";
		}
		return "无数据添加";
	}

	@ResponseBody
	@RequestMapping(value = "parts_return_choose")
	public Object emetalsbom_choose(String buyId) throws Exception {
		List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper.findByAttribute("buyId", buyId,
				PurchaseRequestFormMap.class);
		List<DepartmentForMap> departmentForMaps = departmentMapper.findByWhere(new DepartmentForMap());
		List<SuppFilleFromMap> suppFilleFromMaps = purchaseRequestMapper.getsupplierfille();
		for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {
			MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id", pur.get("metalsBomId").toString(),
					MetalsBomFormMap.class);
			pur.put("metalsBom", metalsBomFormMap);
		}

		for (int i = 0; i < departmentForMaps.size(); i++) {
			for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {
				if (pur.get("departmentId").toString().equals(departmentForMaps.get(i).get("id").toString())) {
					pur.set("departmentId", departmentForMaps.get(i).get("name").toString());
				}
			}
		}

		for (int i = 0; i < suppFilleFromMaps.size(); i++) {
			for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {
				if (pur.get("supplierId").toString().equals(suppFilleFromMaps.get(i).get("suppNumber").toString())) {
					pur.set("supplierId", suppFilleFromMaps.get(i).get("suppName").toString());
				}
			}
		}
		return purchaseRequestFormMaps;
	}

	@ResponseBody
	@RequestMapping("parts_return_add")
	@SystemLog(module = "申请退货", methods = "新增退货") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String parts_return_add() throws Exception {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		String[] buyId = request.getParameterValues("buyId");
		List<DeliverynoteFormMap> deliverynoteFormMap = deliverynoteMapper.findByAttribute("purchaseRequestId",
				buyId[0], DeliverynoteFormMap.class);
		List<Object> list = new ArrayList<Object>();
		List<SuppFilleFromMap> suppFilleFromMap = purchaseRequestMapper.getsupplierfille();
		List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper.findByAttribute("buyId", buyId[0],
				PurchaseRequestFormMap.class);
		if (deliverynoteFormMap.size() != 0) {
			return "采购单已申请进仓";
		}
		String[] supplierId = request.getParameterValues("supplierId");
		String[] departmentId = request.getParameterValues("departmentId");
		String[] materialCode = request.getParameterValues("materialCode");
		String[] number = request.getParameterValues("number");
		String[] total = request.getParameterValues("total");
		String[] comment = request.getParameterValues("comment");

		if (null != supplierId) {
			for (int i = 0; i < supplierId.length; i++) {
				PartsReturnFormMap partsReturnFormMap = new PartsReturnFormMap();
				for (SuppFilleFromMap sup : suppFilleFromMap) {
					if (sup.get("suppName").equals(supplierId[i])) {
						list.add(sup.get("suppNumber"));
					}
				}
				DepartmentForMap departmentForMap = departmentMapper.findbyFrist("name", departmentId[i],
						DepartmentForMap.class);
				partsReturnFormMap.set("deliverDate", DateUtils.getStringDateTime());
				partsReturnFormMap.set("buyId", buyId[0]);
				partsReturnFormMap.set("supplierId", list.get(i));
				partsReturnFormMap.set("departmentId", departmentForMap.get("id"));
				partsReturnFormMap.set("purchaseRequestId", purchaseRequestFormMaps.get(i).get("id"));
				partsReturnFormMap.set("number", number[i]);
				partsReturnFormMap.set("total", total[i]);
				partsReturnFormMap.set("returnReason", comment[0]);
				partsReturnFormMap.set("state", "0");
				partsReturnMapper.addEntity(partsReturnFormMap);
			}
			return "success";
		}
		return "无数据添加";
	}

	@RequestMapping("parts_deliverynote_list")
	public String parts_deliverynote_list(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/metals/parts/parts_deliverynote_list";
	}

	@ResponseBody
	@RequestMapping("parts_deliverynote_findByPage")
	public PageView partsfindByPage(String pageNow, String pageSize) {
		DeliverynoteFormMap deliverynoteFormMap = getFormMap(DeliverynoteFormMap.class);
		deliverynoteFormMap.put("state", "0");
		deliverynoteFormMap = toFormMap(deliverynoteFormMap, pageNow, pageSize, deliverynoteFormMap.getStr("orderby"));
		List<DeliverynoteFormMap> deliverynoteFormMaps = deliverynoteMapper.findByPage(deliverynoteFormMap);
		List<AuditLoggingFormMap> auditLoggingFormMaps = auditLoggingMapper.findByWhere(new AuditLoggingFormMap());

		for (DeliverynoteFormMap del : deliverynoteFormMaps) {
			List<AuditLoggingFormMap> auds = deliverynoteMapper.getDeliverynoteBuyId(del.getInt("purchaseRequestId"));
			if (auds.size() == 3) {
				del.set("state", "审核通过");
				for (AuditLoggingFormMap aud : auds) {
					if (aud.getInt("state") == 2) {
						del.set("state", "未通过");
					}
				}
			} else if (auds.size() != 3) {
				del.set("state", "待审核");
			}
		}

		pageView.setRecords(deliverynoteFormMaps);
		return pageView;
	}

	@ResponseBody
	@RequestMapping("parts_retrun_findByPage")
	public PageView parts_retrun_findByPage(String pageNow, String pageSize) {
		PartsReturnFormMap partsReturnFormMap = getFormMap(PartsReturnFormMap.class);
		partsReturnFormMap.put("state", "0");
		partsReturnFormMap.put("where", "group by buyId");
		partsReturnFormMap = toFormMap(partsReturnFormMap, pageNow, pageSize, partsReturnFormMap.getStr("orderby"));
		List<PartsReturnFormMap> partsReturnFormMaps = deliverynoteMapper.findByPage(partsReturnFormMap);
		List<AuditLoggingFormMap> auditLoggingFormMaps = auditLoggingMapper.findByWhere(new AuditLoggingFormMap());
		List<DepartmentForMap> departmentForMap = departmentMapper.findByWhere(new DepartmentForMap());
		List<SuppFilleFromMap> suppFilleFromMap = purchaseRequestMapper.getsupplierfille();

		for (PartsReturnFormMap aud : partsReturnFormMaps) {
			int auditCategoryId = 2;
			List<AuditLoggingFormMap> auds = auditLoggingMapper.getAuditLoggingByAuditId(aud.get("buyId").toString(),
					auditCategoryId);
			if (auds.size() == 2) {
				aud.set("state", "审核通过");
				for (AuditLoggingFormMap audi : auds) {
					if (audi.getInt("state") == 2) {
						aud.set("state", "未通过");
					}
				}
			} else if (auds.size() != 3) {
				aud.set("state", "待审核");
			}
		}

		for (int i = 0; i < partsReturnFormMaps.size(); i++) {
			for (int j = 0; j < departmentForMap.size(); j++) {
				if (partsReturnFormMaps.get(i).get("departmentId").equals(departmentForMap.get(j).get("id"))) {
					partsReturnFormMaps.get(i).set("departmentId", departmentForMap.get(j).get("name"));
				}
			}
		}

		for (SuppFilleFromMap sup : suppFilleFromMap) {
			for (PartsReturnFormMap par : partsReturnFormMaps) {
				if (sup.get("suppNumber").toString().equals(par.get("supplierId").toString())) {
					par.set("supplierId", sup.get("suppName"));
				}
			}
		}

		pageView.setRecords(partsReturnFormMaps);
		return pageView;
	}

	@RequestMapping("parts_deliverynote_details")
	public String parts_deliverynote_details(Model model) {
		// 获取采购单号
		String id = getPara("purchaseRequestId");
		DeliverynoteFormMap deliverynoteFormMap = deliverynoteMapper.findbyFrist("id", id, DeliverynoteFormMap.class);
		List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper.findByAttribute("buyId",
				deliverynoteFormMap.get("purchaseRequestId").toString(), PurchaseRequestFormMap.class);
		MetalsBomFormMap metalsBomFormMap = new MetalsBomFormMap();
		metalsBomFormMap.put("where", "where state = 0");
		List<MetalsBomFormMap> metalsBomFormMaps = metalsBomMapper.findByWhere(metalsBomFormMap);
		List<MetalsBomFormMap> met = new ArrayList<MetalsBomFormMap>();
		List<UserFormMap> userFormMap = userMapper.findByWhere(new UserFormMap());
		List<UserFormMap> userFormMaps = new ArrayList<UserFormMap>();
		List<AuditLoggingFormMap> auditLoggingFormMaps = auditLoggingMapper.getAuditLoggingByAuditId(id,
				Integer.parseInt("1"));

		for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {
			for (int i = 0; i < metalsBomFormMaps.size(); i++) {
				if (pur.get("metalsBomId").toString().equals(metalsBomFormMaps.get(i).get("id").toString())) {
					metalsBomFormMaps.get(i).put("buyId", deliverynoteFormMap.get("purchaseRequestId").toString());
					metalsBomFormMaps.get(i).put("number", pur.get("number").toString());
					metalsBomFormMaps.get(i).put("price", pur.get("price").toString());
					metalsBomFormMaps.get(i).put("total", pur.get("total").toString());
					metalsBomFormMaps.get(i).put("comment", pur.get("comment").toString());
					met.add(metalsBomFormMaps.get(i));
				}
			}
		}

		for (int i = 0; i < auditLoggingFormMaps.size(); i++) {
			for (int j = 0; j < userFormMap.size(); j++) {
				if (auditLoggingFormMaps.get(i).get("userId").toString()
						.equals(userFormMap.get(j).get("id").toString())) {
					userFormMap.get(j).put("auditTime", auditLoggingFormMaps.get(i).get("auditTime"));
					userFormMaps.add(i, userFormMap.get(j));
				}
			}
		}

		model.addAttribute("metalsBom", met);
		model.addAttribute("deliverynoteFormMap", deliverynoteFormMap);
		return Common.BACKGROUND_PATH + "/metals/parts/parts_deliverynote_details";
	}

	@ResponseBody
	@RequestMapping(value = "parts_deliverynote_del")
	@SystemLog(module = "进仓统计", methods = "进仓-删除进仓单") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		DeliverynoteFormMap deliverynoteFormMap = new DeliverynoteFormMap();

		for (int i = 0; i < str.length; i++) {
			deliverynoteFormMap.set("state", "1");
			deliverynoteFormMap.put("id", str[i]);
			deliverynoteMapper.editEntity(deliverynoteFormMap);
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "parts_return_del")
	@SystemLog(module = "进仓统计", methods = "进仓-删除进仓单") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String partsdelete() throws Exception {
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		PartsReturnFormMap partsReturnFormMap = new PartsReturnFormMap();

		for (int i = 0; i < str.length; i++) {
			PartsReturnFormMap partsReturnFormMap1 = partsReturnMapper.findbyFrist("id", ids[i],
					PartsReturnFormMap.class);
			String buyId = partsReturnFormMap1.get("buyId").toString();
			List<PartsReturnFormMap> partsReturnFormMaps = partsReturnMapper.findByAttribute("buyId", buyId,
					PartsReturnFormMap.class);

			for (PartsReturnFormMap par : partsReturnFormMaps) {
				par.set("state", "1");
				partsReturnMapper.editEntity(par);
			}
		}
		return "success";
	}

	@ResponseBody
	@RequestMapping(value = "parts_deliverynote_add")
	@SystemLog(module = "进仓统计", methods = "进仓-验收进仓单") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String parts_deliverynote_add() throws Exception {
		String id = getPara("purchaseRequestId");
		DeliverynoteFormMap deliverynoteFormMap = deliverynoteMapper.findbyFrist("id", id, DeliverynoteFormMap.class);
		List<AuditLoggingFormMap> auds = deliverynoteMapper
				.getDeliverynoteBuyId(deliverynoteFormMap.getInt("purchaseRequestId"));
		if (auds.size() == 3) {
			deliverynoteFormMap.set("state", "2");
			deliverynoteMapper.editEntity(deliverynoteFormMap);

			List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper.findByAttribute("buyId",
					deliverynoteFormMap.get("purchaseRequestId").toString(), PurchaseRequestFormMap.class);
			for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {
				pur.set("state", "3");
				purchaseRequestMapper.editEntity(purchaseRequestFormMaps);
			}

			MetalsBomFormMap metalsBomFormMap = new MetalsBomFormMap();
			metalsBomFormMap.put("where", "where state=0");
			List<MetalsBomFormMap> metalsBomFormMaps = metalsBomMapper.findByWhere(new MetalsBomFormMap());
			for (int i = 0; i < purchaseRequestFormMaps.size(); i++) {
				for (int j = 0; j < metalsBomFormMaps.size(); j++) {
					if (purchaseRequestFormMaps.get(i).get("metalsBomId").toString()
							.equals(metalsBomFormMaps.get(j).get("id").toString())) {
						int sum1 = metalsBomFormMaps.get(j).getInt("inventoryTotal");
						int sum2 = purchaseRequestFormMaps.get(i).getInt("number");
						int inventoryTotal = sum1 + sum2;
						metalsBomFormMaps.get(j).set("inventoryTotal", inventoryTotal);
						metalsBomMapper.editEntity(metalsBomFormMaps.get(j));

						purchaseRequestFormMaps.get(i).set("state", "3");
						purchaseRequestMapper.editEntity(purchaseRequestFormMaps.get(i));

						PurchaseSesaleStockFromMap sesaleStock = new PurchaseSesaleStockFromMap();
						sesaleStock.set("materialId", metalsBomFormMaps.get(j).get("id"));
						sesaleStock.set("deliveryNoteId", deliverynoteFormMap.get("id"));
						sesaleStock.set("date", DateUtils.getStringDateTime());
						sesaleStock.set("currentInNumber", purchaseRequestFormMaps.get(i).get("number"));
						sesaleStock.set("currentInMoney", purchaseRequestFormMaps.get(i).get("total"));
						sesaleStock.set("state", "0");
						purchaseSaleStockMapper.addEntity(sesaleStock);
					}

				}

			}
			return "success";
		}
		return "false";
	}

	@ResponseBody
	@RequestMapping(value = "parts_return_seach")
	@SystemLog(module = "退货统计", methods = "退货-验收退货单") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String parts_return_seach() throws Exception {
		String id = getPara("buyId");
		PartsReturnFormMap partsReturnFormMap = partsReturnMapper.findbyFrist("id", id, PartsReturnFormMap.class);
		List<PartsReturnFormMap> partsReturnFormMaps = partsReturnMapper.findByAttribute("buyId",
				partsReturnFormMap.get("buyId").toString(), PartsReturnFormMap.class);
		for (PartsReturnFormMap par : partsReturnFormMaps) {
			par.set("state", "2");
			partsReturnMapper.editEntity(par);
		}
		int auditCategoryId = 2;
		List<AuditLoggingFormMap> auds = auditLoggingMapper
				.getAuditLoggingByAuditId(partsReturnFormMap.get("id").toString(), auditCategoryId);

		if (auds.size() == 2) {
			List<PurchaseRequestFormMap> purchaseRequestFormMaps = purchaseRequestMapper.findByAttribute("buyId",
					partsReturnFormMap.get("buyId").toString(), PurchaseRequestFormMap.class);
			for (PurchaseRequestFormMap pur : purchaseRequestFormMaps) {
				pur.set("state", "4");
				purchaseRequestMapper.editEntity(pur);
			}
			return "success";
		}
		return "false";
	}

	@RequestMapping("parts_return_details")
	public String parts_return_details(Model model) {
		// 获取采购单号
		String id = getPara("buyId");
		PartsReturnFormMap partsReturnFormMap = partsReturnMapper.findbyFrist("id", id, PartsReturnFormMap.class);
		List<PartsReturnFormMap> partsReturnFormMaps = partsReturnMapper.findByAttribute("buyId",
				partsReturnFormMap.get("buyId").toString(), PartsReturnFormMap.class);
		List<AuditLoggingFormMap> auditLoggingFormMaps = auditLoggingMapper.getAuditLoggingByAuditId(id,
				Integer.parseInt("2"));
		List<UserFormMap> userFormMap = userMapper.findByWhere(new UserFormMap());
		List<UserFormMap> userFormMaps = new ArrayList<UserFormMap>();
		List<DepartmentForMap> departmentForMap = departmentMapper.findByWhere(new DepartmentForMap());
		List<SuppFilleFromMap> suppFilleFromMap = purchaseRequestMapper.getsupplierfille();

		for (PartsReturnFormMap par : partsReturnFormMaps) {
			PurchaseRequestFormMap purchaseRequestFormMap = purchaseRequestMapper.findbyFrist("id",
					partsReturnFormMap.get("purchaseRequestId").toString(), PurchaseRequestFormMap.class);
			MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id",
					purchaseRequestFormMap.get("metalsBomId").toString(), MetalsBomFormMap.class);

			par.put("metalsBom", metalsBomFormMap);
			par.put("purchaseRequest", purchaseRequestFormMap);
		}

		for (int i = 0; i < auditLoggingFormMaps.size(); i++) {
			for (int j = 0; j < userFormMap.size(); j++) {
				if (auditLoggingFormMaps.get(i).get("userId").toString()
						.equals(userFormMap.get(j).get("id").toString())) {
					userFormMap.get(j).put("auditTime", auditLoggingFormMaps.get(i).get("auditTime"));
					userFormMaps.add(i, userFormMap.get(j));
				}
			}
		}

		for (int i = 0; i < partsReturnFormMaps.size(); i++) {
			for (int j = 0; j < departmentForMap.size(); j++) {
				if (partsReturnFormMaps.get(i).get("departmentId").equals(departmentForMap.get(j).get("id"))) {
					partsReturnFormMaps.get(i).set("departmentId", departmentForMap.get(j).get("name"));
				}
			}
		}

		for (SuppFilleFromMap sup : suppFilleFromMap) {
			for (PartsReturnFormMap par : partsReturnFormMaps) {
				if (sup.get("suppNumber").toString().equals(par.get("supplierId").toString())) {
					par.set("supplierId", sup.get("suppName"));
				}
			}
		}

		model.addAttribute("partsReturnFormMap", partsReturnFormMap);
		model.addAttribute("partsReturnFormMaps", partsReturnFormMaps);
		return Common.BACKGROUND_PATH + "/metals/parts/parts_return_details";
	}

	@ResponseBody
	@RequestMapping("SesaleStock_findByPage")
//	@SystemLog(module = "进销存汇总", methods = "每月汇总") // 凡需要处理业务逻辑的.都需要记录操作日志
	public PageView SesaleStock(String pageNow, String pageSize) throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar date2 = Calendar.getInstance();
		date2.setTime(new Date());
		Date MinDt = new java.util.Date();
		Date MaxDt = new java.util.Date();
		MinDt.setDate(1);
		MinDt.setHours(0);
		MinDt.setMinutes(0);
		MinDt.setSeconds(0);
		MaxDt.setDate(date2.getActualMaximum(Calendar.DAY_OF_MONTH));
		MaxDt.setHours(23);
		MaxDt.setMinutes(59);
		MaxDt.setSeconds(59);
		Calendar c = Calendar.getInstance();
		Calendar y = Calendar.getInstance();
		c.setTime(MaxDt);
		y.setTime(MinDt);
		c.add(Calendar.MONTH, -1);
		y.add(Calendar.MONTH, -1);
		Date max = c.getTime();
		Date min = y.getTime();
		String lastMaxDt = format.format(max);
		String lastMinDt = format.format(min);
		int lastNumber = 0;
		double lastPrice = 0;
		double lastMoney = 0;
		List<Map<String, Object>> purchaseSesaleStocks = new ArrayList<Map<String, Object>>();

		List<PurchaseSesaleStockFromMap> purchaseSesaleStock = purchaseSaleStockMapper
				.getPurchaseSesaleStockDateCheck(lastMinDt, lastMaxDt);

		purchaseSesaleStocks = purchaseSesaleStockFromMap(format.format(MinDt), format.format(MaxDt));
		for (PurchaseSesaleStockFromMap pur : purchaseSesaleStock) {
			for (Map<String, Object> purs : purchaseSesaleStocks) {
				if (pur.get("materialId").toString().equals(purs.get("materialId").toString())) {
					purs.put("initialNumber", pur.get("lastNumber"));
					purs.put("initialPrice", pur.get("lastPrice"));
					purs.put("initialMoney", pur.get("lastMoney"));
					lastNumber = (int) purs.get("initialNumber") + (int) purs.get("currentInNumber")
							- (int) purs.get("currentOutNumber");
					lastMoney = (double) purs.get("initialMoney") + (double) purs.get("currentInMoney")
							- (double) purs.get("currentOutMoney");
					lastPrice = lastMoney / lastNumber;
					purs.put("lastNumber", lastNumber);
					purs.put("lastPrice", lastPrice);
					purs.put("lastMoney", lastMoney);
					if(pur.get("checkNumber")!=null) {
						purs.put("checkNumber", "可编辑");
					}
				}
			}
		}

		LuogicPaging(pageNow, pageSize, purchaseSesaleStocks);
		return pageView;
	}

	@SystemLog(module = "进销存汇总", methods = "每月汇总分离方法")
	public List purchaseSesaleStockFromMap(String MinDt, String MaxDt) {
		List<Map<String, Object>> purchaseSesaleStocks = new ArrayList<Map<String, Object>>();
		List<MetalsBomFormMap> metalsBomFormMaps = metalsBomMapper.findByAttribute("state", "0",
				MetalsBomFormMap.class);

		for (int i = 0; i < metalsBomFormMaps.size(); i++) {
			PurchaseSesaleStockFromMap purchaseSesaleStockFromMap = new PurchaseSesaleStockFromMap();

			List<PurchaseSesaleStockFromMap> purchaseSesaleStocksIN = purchaseSaleStockMapper
					.getPurchaseSesaleStockDateIN(MinDt, MaxDt, metalsBomFormMaps.get(i).getInt("id"));
			int currentInNumber = 0;
			double currentInMoney = 0;
			double currentInPrice = 0;
			if (purchaseSesaleStocksIN.size() > 0) {
				for (PurchaseSesaleStockFromMap pur : purchaseSesaleStocksIN) {
					currentInNumber = currentInNumber + pur.getInt("currentInNumber");
					currentInMoney = currentInMoney + pur.getDouble("currentInMoney");
				}
				currentInPrice = currentInMoney / currentInNumber;
			}

			List<PurchaseSesaleStockFromMap> purchaseSesaleStocksOut = purchaseSaleStockMapper
					.getPurchaseSesaleStockDateOut(MinDt, MaxDt, metalsBomFormMaps.get(i).getInt("id"));
			int currentOutNumber = 0;
			double currentOutMoney = 0;
			double currentOutPrice = 0;
			if (purchaseSesaleStocksOut.size() > 0) {
				for (PurchaseSesaleStockFromMap pur : purchaseSesaleStocksOut) {
					currentOutNumber = currentOutNumber + pur.getInt("currentOutNumber");
					currentOutMoney = currentOutMoney + pur.getDouble("currentOutMoney");
				}
				currentOutPrice = currentOutMoney / currentOutNumber;
			}

			MetalsBomFormMap metalsBomFormMap = metalsBomMapper.findbyFrist("id",
					metalsBomFormMaps.get(i).get("id").toString(), MetalsBomFormMap.class);
			PurchaseSesaleStockFromMap purchaseSesaleStock = purchaseSaleStockMapper.getPurchaseSesaleStockDateCheck2(MinDt, MaxDt, metalsBomFormMap.getInt("id"));
			if(purchaseSesaleStock!=null) {
				purchaseSesaleStockFromMap.put("profitAndLoss", purchaseSesaleStock.get("profitAndLoss"));
			}
			purchaseSesaleStockFromMap.put("materialId", metalsBomFormMap.get("id").toString());
			purchaseSesaleStockFromMap.put("materialCode", metalsBomFormMap.get("materialCode"));
			purchaseSesaleStockFromMap.put("name", metalsBomFormMap.get("name"));
			purchaseSesaleStockFromMap.put("specifications", metalsBomFormMap.get("specifications"));
			purchaseSesaleStockFromMap.put("unit", metalsBomFormMap.get("unit"));
			purchaseSesaleStockFromMap.put("currentInNumber", currentInNumber);
			purchaseSesaleStockFromMap.put("currentInMoney", currentInMoney);
			purchaseSesaleStockFromMap.put("currentInPrice", currentInPrice);
			purchaseSesaleStockFromMap.put("currentOutNumber", currentOutNumber);
			purchaseSesaleStockFromMap.put("currentOutMoney", currentOutMoney);
			purchaseSesaleStockFromMap.put("currentOutPrice", currentOutPrice);
			purchaseSesaleStocks.add(purchaseSesaleStockFromMap);
		}
		return purchaseSesaleStocks;
	}
	
	@ResponseBody
	@RequestMapping(value = "parts_sell_editEntity")
	@SystemLog(module = "进销存", methods = "进销存-编辑盘点") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String parts_sell_editEntity() throws Exception {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Calendar date2 = Calendar.getInstance();
		date2.setTime(new Date());
		Date MinDt = new java.util.Date();
		Date MaxDt = new java.util.Date();
		MinDt.setDate(1);
		MinDt.setHours(0);
		MinDt.setMinutes(0);
		MinDt.setSeconds(0);
		MaxDt.setDate(date2.getActualMaximum(Calendar.DAY_OF_MONTH));
		MaxDt.setHours(23);
		MaxDt.setMinutes(59);
		MaxDt.setSeconds(59);
		Calendar c = Calendar.getInstance();
		Calendar y = Calendar.getInstance();
		c.setTime(MaxDt);
		y.setTime(MinDt);
		c.add(Calendar.MONTH, -1);
		y.add(Calendar.MONTH, -1);
		Date max = c.getTime();
		Date min = y.getTime();
		String lastMaxDt = format.format(max);
		String lastMinDt = format.format(min);
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		UserFormMap userFormMap = (UserFormMap)Common.findUserSession(request);
		PurchaseSesaleStockFromMap pur = new PurchaseSesaleStockFromMap();
		List<MetalsBomFormMap> metalsBomFormMap = metalsBomMapper.findByAttribute("state", "0", MetalsBomFormMap.class);
		String lastNumber = getPara("lastNumber");
		String lastPrice = getPara("lastPrice");
		String lastMoney = getPara("lastMoney");
		String checkNumber = getPara("checkNumber");
		String materialCode = getPara("materialCode");
		if(checkNumber==null) {
			return "false";
		}
		Double profitAndLoss;	
		profitAndLoss = Double.parseDouble(checkNumber) - Double.parseDouble(lastNumber);
		for (MetalsBomFormMap met : metalsBomFormMap) {
			if(met.get("materialCode").equals(materialCode)) {
				PurchaseSesaleStockFromMap purchaseSesaleStock = purchaseSaleStockMapper.getPurchaseSesaleStockDateCheck2(format.format(MinDt), format.format(MaxDt), met.getInt("id"));
				if(purchaseSesaleStock!=null) {
					purchaseSaleStockMapper.deleteByAttribute("id", purchaseSesaleStock.get("id").toString(), PurchaseSesaleStockFromMap.class);
				}
				
				pur.set("materialId", met.get("id"));
				pur.set("lastNumber", lastNumber);
				pur.set("lastPrice", lastPrice);
				pur.set("lastMoney", lastMoney);
				pur.set("checkNumber", checkNumber);
				pur.set("peopleInformationId", userFormMap.getInt("id"));
				pur.set("profitAndLoss", profitAndLoss);
				pur.set("date", format.format(MaxDt));
				pur.set("state", 2);
				purchaseSaleStockMapper.addEntity(pur);
			}
		}		
		return "success";
	}
}
